package one.stand.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jmp.autocofiguration.util.OssUtil2;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CompanyLicenseMapperExt;
import one.stand.model.CompanyLicense;
import one.stand.service.CompanyLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CompanyLicenseServiceImpl implements CompanyLicenseService {
    @Autowired
    private CompanyLicenseMapperExt companyLicenseMapperExt;

    @Override
    @Transactional
    public void add(Integer companyId, String photos, Integer type) {

        photos = OssUtil2.getWebsiteUrlReplace(photos);

        CompanyLicense companyLicense = getByCompanyId(companyId);
        if (Objects.nonNull(companyLicense)) {
            companyLicense.setPhotos(photos);
            companyLicense.setStatus(0);
            companyLicense.setType(type);
            companyLicenseMapperExt.updateByPrimaryKeySelective(companyLicense);
        } else {
            companyLicense = new CompanyLicense();
            companyLicense.setCompanyId(companyId);
            companyLicense.setPhotos(photos);
            companyLicense.setStatus(0);
            companyLicense.setType(type);
            companyLicense.setTypeInit(type);
            companyLicenseMapperExt.insertSelective(companyLicense);
        }
    }

    @Override
    @Transactional
    public void update(Integer id, Integer type, List<String> licensePhotos) {
        CompanyLicense companyLicense = new CompanyLicense();
        companyLicense.setId(id);
        companyLicense.setType(type);
        companyLicense.setPhotos(CollUtil.join(licensePhotos, StrUtil.COMMA));
        companyLicenseMapperExt.updateByPrimaryKeySelective(companyLicense);
    }

    @Override
    @Transactional
    public void audit(Integer id) {
        CompanyLicense companyLicense = new CompanyLicense();
        companyLicense.setId(id);
        companyLicense.setStatus(1);
        companyLicenseMapperExt.updateByPrimaryKeySelective(companyLicense);
    }

    @Override
    @Transactional
    public void refuse(Integer id, String memo) {
        CompanyLicense companyLicense = new CompanyLicense();
        companyLicense.setId(id);
        companyLicense.setStatus(2);
        companyLicense.setMemo(memo);
        companyLicenseMapperExt.updateByPrimaryKeySelective(companyLicense);
    }

    @Override
    public CompanyLicense get(Integer id) {
        return companyLicenseMapperExt.selectByPrimaryKey(id);
    }

    @Override
    public CompanyLicense getByCompanyId(Integer companyId) {
        return companyLicenseMapperExt.selectByCompanyId(companyId);
    }
}
